
#include "config_parser.h"
#include "config_error.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define VERSION "v0.9.1"

typedef int bool;

bool is_comment_line(char*line);		
bool is_in(char* str,char t);
		
#ifdef __linux__
char* strlwr(char* str)
{
    	if(!str)
		return 0;
	int i = 0;
	for(i=0; i<strlen(str); i++)
	{
		if(str[i] >= 0x41 && str[i] <= 0x5a)
			str[i] += 0x20;
	}
	return str;
}
#endif

char* get_version()
{
	return VERSION;
}

CONF_ITEM* init()
{
	CONF_ITEM * ret = (CONF_ITEM*)malloc(sizeof(CONF_ITEM));
	if(!ret)
		return 0;

	ret->name[0] = 0;
	ret->value[0] = 0;
	ret->next = 0;

	return ret;
}
char* get_error_string(int err)
{
	if(err > 0 ) 
		return 0;
	if(err < CP_ERR_BOUND)
		err = CP_ERR_BOUND;

	return _errmsg[abs(err)];
}
int read_config(CONF_ITEM* ctx,const char* filename)
{
	int ret = 0;
	char name_value[2][CONF_NAME_LEN];
	char line[MAX_LINE_CHAR]={0};
	FILE* fp = 0;
	CONF_ITEM* node=0;
	CONF_ITEM* pre =ctx;

	if(!pre)
	{
		return CP_ERR_PARAMETER;
	}

	fp = fopen(filename,"r+t");
	if(!fp)
		return CP_ERR_FILEOPEN;
	
	while(fgets(line,MAX_LINE_CHAR,fp))
	{
		if(1 == is_comment_line(line))
		{
			continue;
		}
		cut_tail(line,COMMENT_CHAR); // Remove comment
		trim(line,COMMENT_BLANK);
		split2(line,'=',name_value);
		
		trim_r(name_value[0],COMMENT_BLANK);
		trim_l(name_value[1],COMMENT_BLANK);
		
		strlwr(name_value[0]);
		
		node = (CONF_ITEM*)malloc(sizeof(CONF_ITEM));
		if(!node)
			return CP_ERR_NOMEMORY;
		strcpy(node->name,name_value[0]);
		strcpy(node->value,name_value[1]);
		node->next = 0;
		pre->next = node;
		pre = node;
	}
	fclose(fp);
	fp = 0 ;

	return CP_ERR_OK;
}
char* get_config(CONF_ITEM* ctx,const char* name)
{
	CONF_ITEM* pt = ctx;
	if(!ctx || !name)
		return 0;
	strlwr((char*)name);
	while(pt)
	{
		if(0 == strcmp(name,pt->name))
		{
			return pt->value;
		}
		else
			pt = pt->next;
	}
	return 0;
}
int deinit(CONF_ITEM* ctx)
{
	CONF_ITEM * pt = ctx;
	CONF_ITEM * pt2 = 0;
	while(pt)
	{
		pt2 = pt->next;
		free(pt);
		pt=pt2;
	}
	return CP_ERR_OK;
}
int trim(char* str,char t)
{
	trim_l(str,t);
	trim_r(str,t);
	return CP_ERR_OK;
}
int trim_l(char* str,char t)
{
	int len;
	int i = 0;
	int index = 0;
	char * temp=0;

	if(!str)
		return CP_ERR_PARAMETER;

	len=strlen(str);

	temp = (char*)malloc(len+1);
	if(!temp)
		return CP_ERR_NOMEMORY;

	for(; i < len; i++)
	{
		if(str[i] == t)
			index++;
		else 
			break;
	}
	if(index > 0)
	{
		strcpy(temp,str+index);
		strcpy(str,temp);
	}

	free(temp);
	temp = 0;

	return CP_ERR_OK;
}
int trim_r(char* str,char t)
{
	int len =0;
	int i=0;
	int index=0;

	if(!str)
		return CP_ERR_PARAMETER;

	len = strlen(str);
	i = len-1;
	index = len;

	for(; i >= 0; i--)
	{
		if(str[i] == t)
			index--;
		else
			break;
	}
	str[index] = 0;

	return CP_ERR_OK;
}

int cut_tail(char* str,char t)
{
	int len=0,i=0;

	if(!str)
		return CP_ERR_PARAMETER;

	len = strlen(str);

	for( i= 0; i < len; i++)
	{
		if(str[i] == t)
		{
			str[i] = 0;
			return CP_ERR_OK;
		}
	}
	return CP_ERR_OK;
}

int split2(char* str,char t,char str1[][CONF_VALUE_LEN])
{
	int len=0,i=0;
	int index = 0;

	if(!str || !str1)
		return CP_ERR_PARAMETER;

	len = strlen(str);

	for( i= 0; i < len; i++)
	{
		if(str[i] == t)
		{
			index = i;
			break;
		}
	}
	
	memcpy(str1[0],str,index);
	str1[0][index] = 0;
	
	memcpy(str1[1],str+index+1,len-index-1);
	str1[1][len-index-1] =0;

	return CP_ERR_OK;
}
/**! Check if line is a comment line
 *  COMMENT LINE must be shows like below:
 *   (Maybe starts with some blank)# some comment words
 *
 *  @line : line string to be check.
 *  @return 1 means yes
 */
bool is_comment_line(char*line)
{
	int i = 0; 
	int len = 0;

	if(!line)
		return 1;
	
	len = strlen(line);
	if(0 == len)
		return 0;
	
	for(i=0; i<len; i++)
	{
		if(line[i] == COMMENT_BLANK)
			continue;
		if(line[i] == COMMENT_CHAR)
			return 1;
		else
			break;
	}
	
	return 0;
}
/**! Check if character 't' appears in string 'str'
  *  @return 1 means yes 
  */
bool is_in(char* str,char t)
{
	int len = 0;
	int i=0;
	if(!str)
		return 0;

	len = strlen(str);

	for(; i < len; i++)
	{
		if(t == str[i])
			return 1;
	}

	return 0;
}
